﻿
namespace Mangosoft.DiceCopy.Application.MainBoundedContext.JobHuntingModule.Services {
    using System;
    using System.Collections.Generic;
    using Mangosoft.DiceCopy.Application.Seedwork;
    using Mangosoft.DiceCopy.Application.MainBoundedContext.DTO;
    using Mangosoft.DiceCopy.Domain.MainBoundedContext.Aggregates.CoverLetterAgg;
    using Mangosoft.DiceCopy.Infrastructure.Crosscutting.Logging;

    public class ResumeAppService : IResumeAppService {
        private readonly ICoverLetterRepository _coverLetterRepository;

        #region Ctor
        public ResumeAppService(ICoverLetterRepository coverLetterRepository) {
            _coverLetterRepository = coverLetterRepository;
        }
        #endregion

        #region CoverLetter's CRUD
        public CoverLetterDTO AddNewCoverLetter(CoverLetterDTO coverLetterDTO) {
            return null;
        }

        public void UpdateCoverLetter(CoverLetterDTO coverLetterDTO) {
            if (coverLetterDTO == null || coverLetterDTO.Id == Guid.Empty)
                throw new ArgumentException("Cannot Update CoverLetter With Empty Information");

            //get persisted item
            var persisted = _coverLetterRepository.Get(coverLetterDTO.Id);

            if (persisted != null) //if coverLetter exist
            {
                //materialize from customer dto
                var current = MaterializeCoverLetterFromDto(coverLetterDTO);

                //Merge changes
                _coverLetterRepository.Merge(persisted, current);

                //commit unit of work
                _coverLetterRepository.UnitOfWork.Commit();
            }
            else
                LoggerFactory.CreateLog().LogWarning("Cannot Update Non ExistingCoverLetter");
        }

        public void RemoveCoverLetter(Guid coverLetterId) {
            var coverLetter = _coverLetterRepository.Get(coverLetterId);

            if (coverLetter != null) //if coverLetter exist
            {
                // remove
                _coverLetterRepository.Remove(coverLetter);

                //commit changes
                _coverLetterRepository.UnitOfWork.Commit();
            }
            else //the customer not exist, cannot remove
                LoggerFactory.CreateLog().LogWarning("CANNOT Remove Non Existing CoverLetter!");
        }

        public CoverLetterDTO FindCoverLetter(Guid coverLetterId) {

            var coverLetter = _coverLetterRepository.Get(coverLetterId);

            if (coverLetter != null) {
                return coverLetter.ProjectedAs<CoverLetterDTO>();
            }
            else
                return null;

        }
        public List<CoverLetterDTO> FindCoverLetters(string text) {
            return null;
        }

        private CoverLetter MaterializeCoverLetterFromDto(CoverLetterDTO coverLetterDTO) {
            return null;
        }
        #endregion



    }
}
